package com.hadoop.mapReduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class WordCountDemoClASS {

    static class Mymapper extends Mapper<LongWritable, Text , Text, LongWritable> {

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String valueStr = value.toString();
            String[] splits = valueStr.split(" ");

            for (int i = 0; i < splits.length; i++) {
                Text keyOut = new Text(splits[i]);
                LongWritable valueOut = new LongWritable(1);
                context.write(keyOut, valueOut);
            }

        }
    }


    static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable> {

        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            long sum = 0;

            for (LongWritable value : values) {
                sum += value.get();
            }

            context.write(key, new LongWritable(sum));

        }
    }

    static class MyPartition extends Partitioner<Text, LongWritable> {

        @Override
        public int getPartition(Text text, LongWritable longWritable, int numPartitions) {

            String keyMapOut = text.toString();

            if (keyMapOut.equals("xiaomi")) {
                return 1;
            } else if (keyMapOut.equals("huawei")) {
                return 0;
            } else if (keyMapOut.equals("iphone7")) {
                return 2;
            } else {
                return 3;
            }


        }
    }


    public static void main(String[] args) throws Exception {


        Configuration configuration = new Configuration();


        //todo 删除已存在的输出目录
        Path outPutPath = new Path(args[1]);
        FileSystem fileSystem = FileSystem.get(configuration);
        if (fileSystem.exists(outPutPath)) {
            fileSystem.delete(outPutPath, true);
            System.out.println("------------file out put is delete------------");
        }


        Job job = Job.getInstance(configuration);

        job.setJobName("sui bian");

        job.setJarByClass(WordCountDemoClASS.class);

        job.setMapperClass(Mymapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        job.setCombinerClass(MyReducer.class);

        job.setPartitionerClass(MyPartition.class);
        job.setNumReduceTasks(4);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));


        //提交并等待执行成功
        int status = job.waitForCompletion(true) ? 0 : 1;

        System.exit(status);


    }


}
